package java3_BasicSyntax;

import java.util.Random;
import java.util.Scanner;

//scanner类，用于终端输入内容
//举例一个猜数字小游戏
public class ScannerDemo {
    public static void main(String[] args) {
        int number = new Random().nextInt(100) + 1;
        System.out.println("猜测的数字是" + number);
        boolean hasGuessed = false;
        System.out.println("欢迎来到猜数字游戏！\n请输入数字：");
        Scanner scanner = new Scanner(System.in);
        int i = 0;
        while (!hasGuessed) {
            int guess;
            try {
                i++;
                String input = scanner.nextLine();
                guess = Integer.parseInt(input);
                if (guess < number) {
                    System.out.println("猜测的数字太小了！");
                } else if (guess > number) {
                    System.out.println("猜测的数字太大了");
                } else {
                    System.out.println("恭喜猜对了，数字是：" + number);
                    hasGuessed = true;
                    System.out.println("一共猜了" + i + "次");
                    switch (i) {
                        case 1:
                            System.out.println("一次就中，真是天才");
                            break;
                        case 2:
                            System.out.println("两次猜中，运气挺不错嘛，要不要去买彩票呢");
                            break;
                        case 3:
                            System.out.println("三次猜中也挺可以的");
                            break;
                        case 4:
                            System.out.println("四次实力也很强");
                            break;
                        case 5:
                            System.out.println("五次还行吧");
                            break;
                        case 6:
                            System.out.println("六次？一点运气都么得了");
                            break;
                        case 7:
                            System.out.println("七次就算了吧");
                            break;
                        default:
                            System.out.println(i + "次才猜中？您真是不可多得的小废物呢！");
                    }
                }
            } catch (Exception e) {
                System.out.println("要输入数字！不要乱输入，知不知道什么叫数字！");
            }
        }
        scanner.close();

    }
}
